package com.company.doubleIndex;

public class PalindromeNum {

    public boolean isPalindrome(int x) {
//        return byStringArray(x);
        if (x<0) {
            return false;
        }
        return byInteger(Math.abs(x));
    }

    private boolean byInteger(int x){
        int numLength = 0;
        for (;; numLength++) {
            int pow = (int)Math.pow(10, numLength);
            if (x/pow == 0||pow ==2147483647) {
                break;
            }
        }
        if (numLength<=1) {
            return true;
        }
        int offset = 0;

        while (offset<=numLength/2){
            //取前面第offset位的数字
            int hn= getNumByPosition(x,numLength-offset);
            //取后面第offset位的数字
            int ln= getNumByPosition(x,offset+1);
            System.out.println("hn = "+hn  +"   ln="+ln);
            if (hn == ln) {
                offset++;
                continue;
            }
            return false;
        }
        return true;
    }

    private int getNumByPosition(int x,int position){
        //首先 将x目标位移动到个位
        System.out.println("position = "+position);
        x = x/((int)Math.pow(10,position-1));
        System.out.println("x = "+x);
        return x %10;
    }

    private boolean byStringArray(int x){
        String xStr = x+"";

        int left=0;
        int right=xStr.length()-1;

        while (left<=right){
            char lc = xStr.charAt(left);
            char rc = xStr.charAt(right);

            if (lc == rc) {
                left++;
                right--;
                continue;
            }
            return false;
        }
        return true;
    }
}
